package annotation.load;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ClassStructure {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class user = Class.forName("annotation.reflect.User");

        //获得类的名字
        System.out.println(user.getName());//获得包名+类名
        System.out.println(user.getSimpleName());//获得类名

        System.out.println();
        //获得类的属性
        Field[] fields = user.getFields();//只能找到public属性
        for (Field field : fields) {
            System.out.println(field);
        }
        fields = user.getDeclaredFields();//找到全部的属性
        for (Field field : fields) {
            System.out.println(field);
        }

        System.out.println();
        //获得指定类属性
        Field name = user.getDeclaredField("name");
        System.out.println(name);

        System.out.println();
        //获得类方法
        Method[] methods = user.getMethods();
        for (Method method : methods) {
            System.out.println("正常："+method);//获得本类及其父类的全部public方法
        }
        methods = user.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("Declare："+method);//获得本类的全部方法
        }

        System.out.println();
        //获得指定类方法
        Method getName = user.getMethod("getName", null);
        Method setName = user.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);

        System.out.println();
        //获得类构造器
        Constructor[] constructors = user.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("普通："+constructor);
        }
        constructors = user.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("Declare："+constructor);
        }

        System.out.println();
        //获得指定类构造器
        Constructor constructor = user.getConstructor();
        System.out.println(constructor);
    }
}
